package com.mikufufu.blog.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mikufufu.blog.common.entity.AjaxResult;
import com.mikufufu.blog.model.entity.SysSetting;
import com.mikufufu.blog.model.vo.admin.EmailConfig;
import com.mikufufu.blog.model.vo.api.WebSiteVO;
import com.mikufufu.blog.service.SysSettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/sysSetting")
@Api(tags = "系统设置接口")
public class SysSettingAdminController {

    @Autowired
    private SysSettingService sysSettingService;

    @GetMapping("/getByCode")
    @ApiOperation(value = "获取指定编码的系统设置信息")
    @ApiImplicitParam(name = "code", value = "配置编号", required = true)
    public AjaxResult<SysSetting> get(String code){
        // 根据配置编号查询并返回对应的系统设置信息
        return AjaxResult.data(sysSettingService.getOne(new LambdaQueryWrapper<SysSetting>().eq(SysSetting::getCode,code)));
    }

    @GetMapping("/getStorageCode")
    @ApiOperation(value = "获取系统设置中的存储桶编码")
    @PreAuthorize("@ss.hasPermission('sys:setting:storage')")
    public AjaxResult<String> getStorageCode() {
        return AjaxResult.data(sysSettingService.getStorageCode());
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增存储桶设置到系统设置中")
    public AjaxResult<Boolean> add(@RequestBody SysSetting setting) {
        return AjaxResult.status(sysSettingService.save(setting));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改系统设置")
    public AjaxResult<Boolean> update(@RequestBody SysSetting setting) {
        return AjaxResult.status(sysSettingService.updateById(setting));
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除系统设置")
    @ApiImplicitParam(name = "code", value = "系统设置的编码", required = true)
    public AjaxResult<Boolean> delete(String code) {
        return AjaxResult.status(sysSettingService.remove(new LambdaQueryWrapper<SysSetting>().eq(SysSetting::getCode,code)));
    }

    @PostMapping("/saveWebInfo")
    @ApiOperation(value = "新增或修改网站信息到系统设置中")
    public AjaxResult<Boolean> saveWebInfo(@RequestBody WebSiteVO webSiteVO) {
        return AjaxResult.status(sysSettingService.saveWebInfo(webSiteVO));
    }

    @GetMapping("/getEmail")
    @ApiOperation(value = "获取邮件设置")
    public AjaxResult<EmailConfig> getEmail() {
        EmailConfig emailConfig = sysSettingService.getMail();
        return AjaxResult.data(emailConfig);
    }

    @PostMapping("/setEmail")
    @ApiOperation(value = "设置邮件设置")
    public AjaxResult<Boolean> setEmail(@RequestBody EmailConfig emailConfig) {
        return AjaxResult.status(sysSettingService.setMail(emailConfig));
    }
}
